﻿//
//  ThemesListViewController.m
//  WDFans
//
//  Created by Zhihong Yuan on 2/15/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ThemesListViewController.h"
#import "ThemesTableCell.h"
#import "MoreButtonCell.h"

#import "DeliverThemeViewController.h"
#import "ZHDataService.h"
#import "ZHWebService.h"
#import "WDThemes.h"
#import "WDForum.h"
#import "WDFansAppDelegate.h"

static NSInteger nextPage = 1;

@interface ThemesListViewController()

@property (nonatomic, retain) NSMutableArray *themes;
@property (nonatomic, assign) BOOL isNextPageExist;

@end

@implementation ThemesListViewController
@synthesize themes, isNextPageExist;
@synthesize forum;
@synthesize detailThemeController;



#pragma mark - Ryan methods
- (void)getAllThemesIsNext:(BOOL)isNext
{
    CompletionBlock completion = ^(NSData *data){
        NSDictionary *dict = [ ZHDataService parseToThemesWithData:data];
        self.isNextPageExist = [[dict objectForKey:@"isNextPageExist"] intValue];
        if(!isNext)
            self.themes = [dict objectForKey:@"themes"];
        else
            [self.themes addObjectsFromArray:[dict objectForKey:@"themes"]];
        [self.myTableView reloadData];
    };
    ErrorBlock error = ^(NSString *error){};
    [[ZHWebService sharedService] getThemesWithSectionId:self.forum.forumId page:nextPage onCompletion:completion onError:error];
}

#pragma mark - BaseTableViewController methods
- (void)rightItemClicked
{
    if(![ZHDataService isLogined])
    {
        UIAlertView *alertView  = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登陆" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertView show];
        [alertView release];
        
        return;
    }
    
    DeliverThemeViewController* vc = [[DeliverThemeViewController alloc]initWithNibName:@"DeliverThemeViewController"  bundle:nil];
    vc.viewType = kDeliverType;
    vc.titleName = @"";
//    vc.tid = tid;
    vc.fid = self.forum.forumId;
    [self presentModalViewController:vc animated:YES];
    
    [vc release];
}

#pragma mark - View lifecycle
- (void)dealloc
{
    
    detailThemeController.delegate = nil;
    [detailThemeController release];
    
    [forum release];
    [themes release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"帖子列表";
    [self setBackgroundImageViewForTabView];
    [self addNaviBarRightItemWithTitle:@"发帖"];
    [self getAllThemesIsNext:NO];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.themes = nil;
}

#pragma mark - UITableViewDatasource methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
        return kThemesTableCellHeight;
    else
        return kMoreButtonCellHeight;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return [NSString stringWithFormat:@"共有%d帖子",[self.themes count]];
    }
    return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if(self.isNextPageExist)
        return 2;
    else 
        return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
        return [self.themes count];
    else
        return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0)
    {
        static NSString *CellIdentifier = @"CellIdentifier";
        ThemesTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"ThemesTableCell" owner:self options:nil];
            cell = (ThemesTableCell *)[nibs lastObject];
        }
        WDThemes *t = [self.themes objectAtIndex:indexPath.row];
        cell.themeNameLabel.text = t.subject;
        cell.themeRegionLabel.text = self.forum.name;
        cell.themeLegalLabel.text = t.lastPoster;
        cell.themeDateLabel.text = t.dateLine;
        return cell;
    }
    else
    {
        static NSString *moreButtonCellIdentifier = @"CellIdentifier";
        MoreButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:moreButtonCellIdentifier];
        if (cell == nil) {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"MoreButtonCell" owner:self options:nil];
            cell = (MoreButtonCell *)[nibs lastObject];
        }
        return cell;
    }
}

#pragma mark - UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {

        if (detailThemeController!=nil) {
            detailThemeController.delegate = nil;
            [detailThemeController release];
            detailThemeController = nil;
        }
        self.detailThemeController = [[DetailThemeViewController alloc] init];
        WDThemes *t = [self.themes objectAtIndex:indexPath.row];
        detailThemeController.tid = t.tid;
        detailThemeController.fid = t.fid;
        detailThemeController.view.tag = 1;
        detailThemeController.delegate = self;
        
        WDFansAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
        
        [appDelegate.window addSubview:detailThemeController.view];
        
        detailThemeController.view.frame = CGRectMake(320, 20, 320, 460);
        detailThemeController.view.alpha = 0.3;
        
        [UIView beginAnimations:@"" context:nil];
        [UIView setAnimationDelegate:nil];
        [UIView setAnimationDuration:0.3];
        detailThemeController.view.frame = CGRectMake(0, 20, 320, 460);
        detailThemeController.view.alpha = 1.0;
        [UIView commitAnimations];
        
        
    }
    else
    {
        nextPage++;
        [self getAllThemesIsNext:YES];
    }
}

#pragma mark -
#pragma mark Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource{
    [super reloadTableViewDataSource];
    nextPage = 1;
    [self getAllThemesIsNext:NO];
}

-(void)removeView
{
    WDFansAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
    
    UIView* view = [appDelegate.window viewWithTag:1];
    [view removeFromSuperview];

}


- (void)backClick
{
    WDFansAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
    UIView* view = [appDelegate.window viewWithTag:1];
    
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDidStopSelector:@selector(removeView)];
    view.frame = CGRectMake(320, 20, 320, 460);
    view.alpha = 0.3;
    [UIView commitAnimations];
    
    
}

@end
